
// 引用api文件
const api = require('../../utils/api.js');
// 引入封装的请求类
const {myRequest} = require('../../utils/myRequest.js');
//在使用的View中引入WxParse模块
var WxParse = require('../../libs/wxParse/wxParse');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    banner:[],        //广告位数据
    newGoodsList:[],  //新品列表
    hotGoodsList:[],  //热卖列表
    brandList:[],     //品牌列表
    topicList:[],     //话题列表
    categoryList:[],  //分类
    goodsList: [],    //商品列表
    num: 0            //下标
  },

  // 下标选择
  toGoods(e){
    let index = e.currentTarget.dataset.id;
    console.log(index);
    this.setData({
      num: index,
      goodsList: this.data.categoryList[index].goodsList
    })
  },

  // 页面跳转 传递商品id  
  onNewGoods(e){
    let id = e.currentTarget.dataset.newgoodsid;
    console.log('onNewGoods_id',id);
    wx.navigateTo({
      url: `/Paypages/pages/good_detail/good_detail?NewGoods_id=${id}`,
    })
  },
  onHotGoods(e){
    let id = e.currentTarget.dataset.hotgoodsid;
    console.log('onHotGoods_id',id);
    wx.navigateTo({
      url: `/Paypages/pages/good_detail/good_detail?HotGoods_id=${id}`,
    })
  },
  onBrandList(e){ 
    let id = e.currentTarget.dataset.brandlistid;
    console.log('onBrandList_id',id);
    wx.navigateTo({
      url: `/Paypages/pages/brandDetail/brandDetail?BrandId=${id}`,
    })
  },
  onGoodsList(e){
    let id = e.currentTarget.dataset.goodslistid;
    console.log('onGoodsList_id',id);
    wx.navigateTo({
      url: `/Paypages/pages/good_detail/good_detail?GoodsList_id=${id}`,
    })
  },
  onTopicList(e){
    let id = e.currentTarget.dataset.topiclistid;
    console.log('onTopicList_id',id);
    wx.navigateTo({
      url: `/Paypages/pages/topic/topic?TopicList_id=${id}`,
    })
  },

  // 点击 跳转新品页面
  toNewGoodsPage(){
    wx.navigateTo({
      url: '../newGoods/newGoods',
    })
  },

  // 点击 跳转热门商品页面
  toHotGoodsPage(){
    wx.navigateTo({
      url: '../hotGoods/hotGoods'
    })
  },

  // 点击 跳转商品列表页面
  toBrandList(){
    wx.navigateTo({
      url: '../brandGoods/brandGoods'
    })
  },

  // 点击 跳转搜索页面
  toSearchPage(){
    wx.navigateTo({
      url: '../search/search'
    })
  },

  // 点击 跳转到专题列表页
  toTopicList(){
    wx.navigateTo({
      url: '../topicList/topicList'
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    // 请求接口获取渲染页面的数据
    myRequest({
      url: api.index,
      success: (res)=>{
        console.log('index数据',res);
        let data = res.data.data;
        this.setData({
          banner: data.banner,
          newGoodsList: data.newGoodsList,
          hotGoodsList: data.hotGoodsList,
          brandList: data.brandList,
          topicList: data.topicList,
          categoryList: data.categoryList,
          goodsList: data.categoryList[0].goodsList
        });
        
        // wxParse文本解析
        var content = data.topicList[0].content;
        WxParse.wxParse('content', 'html', content, this, 5);
      }
    });
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    wx.showToast({
      title: '没有更多内容啦~',
      icon: 'none',
      duration: 1000
    })
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

})
